﻿// Filename: ServiceDetailBUS.cs
// Author: Giã Dương Đức Minh
// Email: giaduongducminh@gmail.com
// 
// Date created: 04/01/2012 (dd/mm/yyyy)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace Ryder_US_Website.BUS
{
    /// <summary>
    /// Ryder_US_Website.BUS.ServiceDetailBUS
    /// 
    /// Team Breakers
    /// Author: Đức Minh GIÃ DƯƠNG (Breakers.Terry)
    /// Email: giaduongducminh@gmail.com
    /// </summary>
    public class ServiceDetailBUS
    {
        //public static Breakers_RyderUSDataContext context =
        //    new Breakers_RyderUSDataContext(ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString);
        public static string conString = ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString;

        #region Select
        /// <summary>
        /// Gets all.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetAll(int curPage, int pageSize, ref int numPages)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (curPage < 1)
                    curPage = 1;
                if (pageSize < 1)
                    pageSize = 10;
                var query = from sd in context.RUS_ServiceDetails
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        public static List<RUS_ServiceDetail> GetAll()
        {
            List<RUS_ServiceDetail> list = new List<RUS_ServiceDetail>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            select sd;
                list = query.ToList<RUS_ServiceDetail>();
            }
            return list;
        }

        /// <summary>
        /// Gets all active service details.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetAllActive(int curPage, int pageSize, ref int numPages)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (curPage < 1)
                    curPage = 1;
                if (pageSize < 1)
                    pageSize = 10;
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_trash == 0
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        /// <summary>
        /// Gets all inactive service detail.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetAllInactive(int curPage, int pageSize, ref int numPages)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (curPage < 1)
                    curPage = 1;
                if (pageSize < 1)
                    pageSize = 10;
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_trash == 1
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        /// <summary>
        /// Gets all service detail by service.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <param name="service">The service.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetAllByService(int curPage, int pageSize, ref int numPages, RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (service == null)
                    return new List<RUS_ServiceDetail>();
                if (curPage < 1)
                    curPage = 1;
                if (pageSize < 1)
                    pageSize = 10;
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ServiceID == service.Service_ID
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        /// <summary>
        /// Gets the active service details by service.
        /// </summary>
        /// <param name="curPage">The cur page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The num pages.</param>
        /// <param name="service">The service.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetActiveByService(int curPage, int pageSize, ref int numPages, RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (service == null)
                    return new List<RUS_ServiceDetail>();
                if (curPage < 1)
                    curPage = 1;
                if (pageSize < 1)
                    pageSize = 10;
                var query = from sd in context.RUS_ServiceDetails
                            where (sd.ServiceDetail_ServiceID == service.Service_ID && sd.ServiceDetail_trash == 0)
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        /// <summary>
        /// Gets the inactive service details by service.
        /// </summary>
        /// <param name="curPage">The cur page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The num pages.</param>
        /// <param name="service">The service.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetInactiveByService(int curPage, int pageSize, ref int numPages, RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (service == null)
                    return new List<RUS_ServiceDetail>();
                if (curPage < 1)
                    curPage = 1;
                if (pageSize < 1)
                    pageSize = 10;
                var query = from sd in context.RUS_ServiceDetails
                            where (sd.ServiceDetail_ServiceID == service.Service_ID && sd.ServiceDetail_trash == 1)
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        /// <summary>
        /// Gets the active service details by service.
        /// </summary>
        /// <param name="curPage">The cur page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The num pages.</param>
        /// <param name="serviceID">The service ID.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetActiveByService(int curPage, int pageSize, ref int numPages, int serviceID)
        {
            if (curPage < 1)
                curPage = 1;
            if (pageSize < 1)
                pageSize = 10;
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where (sd.ServiceDetail_ServiceID == serviceID && sd.ServiceDetail_trash == 0)
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        /// <summary>
        /// Gets the inactive service details by service.
        /// </summary>
        /// <param name="curPage">The cur page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="numPages">The num pages.</param>
        /// <param name="serviceID">The service ID.</param>
        /// <returns></returns>
        public static List<RUS_ServiceDetail> GetInactiveByService(int curPage, int pageSize, ref int numPages, int serviceID)
        {
            if (curPage < 1)
                curPage = 1;
            if (pageSize < 1)
                pageSize = 10;
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where (sd.ServiceDetail_ServiceID == serviceID && sd.ServiceDetail_trash == 1)
                            select sd;
                numPages = (query.Count() / pageSize) - 1;
                return query.Skip((curPage - 1) * pageSize).Take(pageSize).ToList<RUS_ServiceDetail>();
            }
        }

        /// <summary>
        /// Gets the service detail.
        /// </summary>
        /// <param name="id">The service detail id.</param>
        /// <returns></returns>
        public static RUS_ServiceDetail GetServiceDetail(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ID == id
                            select sd;
                return query.SingleOrDefault<RUS_ServiceDetail>();
            }
        }
        #endregion

        #region Insert
        /// <summary>
        /// Inserts the specified service detail.
        /// </summary>
        /// <param name="serviceDetail">The service detail.</param>
        public static RUS_ServiceDetail Insert(RUS_ServiceDetail serviceDetail)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (serviceDetail == null)
                    return null;
                context.RUS_ServiceDetails.InsertOnSubmit(serviceDetail);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể thêm dịch vụ mới được.");
                }
            }
            return serviceDetail;
        }

        /// <summary>
        /// Inserts the specified service detail.
        /// </summary>
        /// <param name="serviceID">The service ID.</param>
        /// <param name="name">The name.</param>
        /// <param name="unit">The unit.</param>
        /// <param name="min">The min.</param>
        /// <param name="max">The max.</param>
        /// <param name="price">The price.</param>
        /// <param name="description">The description.</param>
        public static RUS_ServiceDetail Insert(int serviceID, String name, String unit, int min, int max, Decimal price, String description)
        {
            if (ServiceBUS.GetActiveServiceByID(serviceID).Service_ID == 0)
                throw new Exception("Dịch vụ này không tồn tại");
            if (String.IsNullOrEmpty(name))
                throw new Exception("Tên chi tiết dịch vụ không được bỏ trống.");
            if (String.IsNullOrEmpty(unit))
                throw new Exception("Đơn vị không được bỏ trống.");
            if (min < 0)
                throw new Exception("Đơn vị tối thiểu phải lớn hơn hoặc bằng 0.");
            if (max < 0)
                throw new Exception("Đơn vị tối đa phải lớn hơn hoặc bằng 0.");
            if (max < min)
                throw new Exception("Đơn vị tối đa phải lớn hơn đơn vị tối thiểu.");
            if (price < 0)
                throw new Exception("Giá phải lớn hơn 0.");
            RUS_ServiceDetail serviceDetail = new RUS_ServiceDetail();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                serviceDetail.ServiceDetail_ServiceID = serviceID;
                serviceDetail.ServiceDetail_name = name;
                serviceDetail.ServiceDetail_unit = unit;
                serviceDetail.ServiceDetail_unitMin = min;
                serviceDetail.ServiceDetail_unitMax = max;
                serviceDetail.ServiceDetail_price = price;
                serviceDetail.ServiceDetail_description = description;
                serviceDetail.ServiceDetail_trash = 0;
                context.RUS_ServiceDetails.InsertOnSubmit(serviceDetail);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể thêm dịch vụ mới được.");
                }
            }
            return serviceDetail;
        }

        /// <summary>
        /// Inserts the specified service detail.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="name">The name.</param>
        /// <param name="unit">The unit.</param>
        /// <param name="min">The min.</param>
        /// <param name="max">The max.</param>
        /// <param name="price">The price.</param>
        /// <param name="description">The description.</param>
        public static RUS_ServiceDetail Insert(RUS_Service service, String name, String unit, int min, int max, Decimal price, String description)
        {
            if (ServiceBUS.GetActiveServiceByID(service.Service_ID).Service_ID == 0)
                throw new Exception("Dịch vụ này không tồn tại");
            if (String.IsNullOrEmpty(name))
                throw new Exception("Tên chi tiết dịch vụ không được bỏ trống.");
            if (String.IsNullOrEmpty(unit))
                throw new Exception("Đơn vị không được bỏ trống.");
            if (min < 0)
                throw new Exception("Đơn vị tối thiểu phải lớn hơn hoặc bằng 0.");
            if (max < 0)
                throw new Exception("Đơn vị tối đa phải lớn hơn hoặc bằng 0.");
            if (max < min)
                throw new Exception("Đơn vị tối đa phải lớn hơn đơn vị tối thiểu.");
            if (price < 0)
                throw new Exception("Giá phải lớn hơn 0.");
            RUS_ServiceDetail serviceDetail = new RUS_ServiceDetail();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                serviceDetail.ServiceDetail_ServiceID = service.Service_ID;
                serviceDetail.ServiceDetail_name = name;
                serviceDetail.ServiceDetail_unit = unit;
                serviceDetail.ServiceDetail_unitMin = min;
                serviceDetail.ServiceDetail_unitMax = max;
                serviceDetail.ServiceDetail_price = price;
                serviceDetail.ServiceDetail_description = description;
                serviceDetail.ServiceDetail_trash = 0;
                context.RUS_ServiceDetails.InsertOnSubmit(serviceDetail);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể cập nhật dịch vụ được.");
                }
            }
            return serviceDetail;
        }
        #endregion

        #region Update
        /// <summary>
        /// Updates the specified service detail.
        /// </summary>
        /// <param name="serviceID">The service ID.</param>
        /// <param name="name">The name.</param>
        /// <param name="unit">The unit.</param>
        /// <param name="min">The min.</param>
        /// <param name="max">The max.</param>
        /// <param name="price">The price.</param>
        /// <param name="description">The description.</param>
        /// <param name="trash">The trash.</param>
        /// <param name="id">The id.</param>
        public static RUS_ServiceDetail Update(int serviceID, String name, String unit, int min, int max, Decimal price, String description, int trash, int id)
        {
            if (ServiceBUS.GetActiveServiceByID(serviceID).Service_ID == 0)
                throw new Exception("Dịch vụ này không tồn tại");
            if (String.IsNullOrEmpty(name))
                throw new Exception("Tên chi tiết dịch vụ không được bỏ trống.");
            if (String.IsNullOrEmpty(unit))
                throw new Exception("Đơn vị không được bỏ trống.");
            if (min < 0)
                throw new Exception("Đơn vị tối thiểu phải lớn hơn hoặc bằng 0.");
            if (max < 0)
                throw new Exception("Đơn vị tối đa phải lớn hơn hoặc bằng 0.");
            if (max < min)
                throw new Exception("Đơn vị tối đa phải lớn hơn đơn vị tối thiểu.");
            if (price < 0)
                throw new Exception("Giá phải lớn hơn 0.");
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ID == id
                            select sd;
                RUS_ServiceDetail serviceDetail = query.SingleOrDefault<RUS_ServiceDetail>();
                if (serviceDetail != null)
                {
                    serviceDetail.ServiceDetail_ServiceID = serviceID;
                    serviceDetail.ServiceDetail_name = name;
                    serviceDetail.ServiceDetail_unit = unit;
                    serviceDetail.ServiceDetail_unitMin = min;
                    serviceDetail.ServiceDetail_unitMax = max;
                    serviceDetail.ServiceDetail_price = price;
                    serviceDetail.ServiceDetail_description = description;
                    serviceDetail.ServiceDetail_trash = trash;
                }
                try
                {
                    //context.RUS_ServiceDetails.Attach(serviceDetail);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể cập nhật dịch vụ được.");
                }
                return serviceDetail;
            }
        }
        #endregion

        #region Delete & restore
        /// <summary>
        /// Deletes the specified service detail.
        /// </summary>
        /// <param name="serviceDetail">The service detail.</param>
        public static RUS_ServiceDetail Delete(RUS_ServiceDetail serviceDetail)
        {
            if (serviceDetail == null)
                throw new Exception("Chi tiết dịch vụ này không tồn tại.");
            serviceDetail.ServiceDetail_trash = 1;
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                try
                {
                    context.RUS_ServiceDetails.Attach(serviceDetail);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
            }
            return serviceDetail;
        }

        /// <summary>
        /// Restores the specified service detail.
        /// </summary>
        /// <param name="serviceDetail">The service detail.</param>
        public static RUS_ServiceDetail Restore(RUS_ServiceDetail serviceDetail)
        {
            if (serviceDetail == null)
                throw new Exception("Chi tiết dịch vụ này không tồn tại.");
            serviceDetail.ServiceDetail_trash = 0;
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                try
                {
                    context.RUS_ServiceDetails.Attach(serviceDetail);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
            }
            return serviceDetail;
        }

        /// <summary>
        /// Deletes the specified service detail by id.
        /// </summary>
        /// <param name="id">The id.</param>
        public static RUS_ServiceDetail Delete(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ID == id
                            select sd;
                RUS_ServiceDetail serviceDetail = query.SingleOrDefault<RUS_ServiceDetail>();
                if (serviceDetail == null || serviceDetail.ServiceDetail_ID == 0)
                    throw new Exception("Chi tiết dịch vụ này không tồn tại.");
                serviceDetail.ServiceDetail_trash = 1;
                try
                {
                    context.RUS_ServiceDetails.Attach(serviceDetail);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
                return serviceDetail;
            }
        }

        /// <summary>
        /// Restores the specified service detail by id.
        /// </summary>
        /// <param name="id">The id.</param>
        public static RUS_ServiceDetail Restore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ID == id
                            select sd;
                RUS_ServiceDetail serviceDetail = query.SingleOrDefault<RUS_ServiceDetail>();
                if (serviceDetail == null || serviceDetail.ServiceDetail_ID == 0)
                    throw new Exception("Chi tiết dịch vụ này không tồn tại.");
                serviceDetail.ServiceDetail_trash = 0;
                try
                {
                    context.RUS_ServiceDetails.Attach(serviceDetail);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return serviceDetail;
            }
        }

        /// <summary>
        /// Deletes all service details by service.
        /// </summary>
        /// <param name="service">The service.</param>
        public static List<RUS_ServiceDetail> DeleteAllByService(RUS_Service service)
        {
            if (service == null)
                return null;
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ServiceID == service.Service_ID && sd.ServiceDetail_trash == 0
                            select sd;
                List<RUS_ServiceDetail> list = query.ToList<RUS_ServiceDetail>();
                foreach (RUS_ServiceDetail sd in list)
                    sd.ServiceDetail_trash = 1;
                try
                {
                    context.RUS_ServiceDetails.AttachAll<RUS_ServiceDetail>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
                return list;
            }
        }

        /// <summary>
        /// Restores all service details by service.
        /// </summary>
        /// <param name="service">The service.</param>
        public static List<RUS_ServiceDetail> RestoreAllByService(RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (service == null)
                    return null;
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ServiceID == service.Service_ID && sd.ServiceDetail_trash == 1
                            select sd;
                List<RUS_ServiceDetail> list = query.ToList<RUS_ServiceDetail>();
                foreach (RUS_ServiceDetail sd in list)
                    sd.ServiceDetail_trash = 0;
                try
                {
                    context.RUS_ServiceDetails.AttachAll<RUS_ServiceDetail>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return list;
            }
        }

        /// <summary>
        /// Deletes all service details by service ID.
        /// </summary>
        /// <param name="serviceID">The service ID.</param>
        public static List<RUS_ServiceDetail> DeleteAllByService(int serviceID)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ServiceID == serviceID && sd.ServiceDetail_trash == 0
                            select sd;
                List<RUS_ServiceDetail> list = query.ToList<RUS_ServiceDetail>();
                foreach (RUS_ServiceDetail sd in list)
                    sd.ServiceDetail_trash = 1;
                try
                {
                    context.RUS_ServiceDetails.AttachAll<RUS_ServiceDetail>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
                return list;
            }
        }

        /// <summary>
        /// Restores all service details by service  ID.
        /// </summary>
        /// <param name="serviceID">The service ID.</param>
        public static List<RUS_ServiceDetail> RestoreAllByService(int serviceID)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from sd in context.RUS_ServiceDetails
                            where sd.ServiceDetail_ServiceID == serviceID && sd.ServiceDetail_trash == 1
                            select sd;
                List<RUS_ServiceDetail> list = query.ToList<RUS_ServiceDetail>();
                foreach (RUS_ServiceDetail sd in list)
                    sd.ServiceDetail_trash = 0;
                try
                {
                    context.RUS_ServiceDetails.AttachAll<RUS_ServiceDetail>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return list;
            }
        }

        /// <summary>
        /// Deletes the restore.
        /// </summary>
        /// <param name="serviceDetail">The service detail.</param>
        /// <returns></returns>
        public static RUS_ServiceDetail DeleteRestore(RUS_ServiceDetail serviceDetail)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (serviceDetail == null)
                    throw new Exception("Chi tiết dịch vụ này không tồn tại.");
                String exception = "Có lỗi xảy ra! Không thể xóa dịch vụ được.";
                if (serviceDetail.ServiceDetail_trash == 1)
                    exception = "Có lỗi xảy ra! Không thể phục hồi dịch vụ được.";
                serviceDetail.ServiceDetail_trash = 1 - serviceDetail.ServiceDetail_trash;
                try
                {
                    context.RUS_ServiceDetails.Attach(serviceDetail);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception(exception);
                }
                return serviceDetail;
            }
        }

        /// <summary>
        /// Deletes or restores ServiceDetail.
        /// </summary>
        /// <param name="id">The id.</param>
        public static RUS_ServiceDetail DeleteRestore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_ServiceDetails
                            where (s.ServiceDetail_ID == id)
                            select s;
                RUS_ServiceDetail srd = query.SingleOrDefault<RUS_ServiceDetail>();
                if (srd.ServiceDetail_trash == 0)
                {
                    foreach (RUS_Cargo sd in srd.RUS_Cargos)
                    {
                        if (sd.Cargo_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                    }
                }
                srd.ServiceDetail_trash = 1 - srd.ServiceDetail_trash;
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return srd;
            }
        }
        #endregion
    }
}
